<?php

namespace Biz\service\search;

use Biz\model\GoodsBasicInfo;
use Doraemon\pockets\config\IniConfig;
use Doraemon\tools\Curl;
use Doraemon\tools\HhzEnv;
/**
 * @deprecated
 */
abstract class BaseSearch
{
    const SOLR_SECTION = NULL;
    const DEFAULT_PAGESIZE = 30;

    public static function getBasicUrl()
    {
        $config = IniConfig::getConfigSelect('search_mod', static::SOLR_SECTION);
        return $config['host'] . "/";
    }

    public static function getSearchUrl()
    {
        return self::getBasicUrl() . 'list';
    }

    public static function searchByFields(array $where, array $fields = ['id'], $page = 1, $pagesize = self::DEFAULT_PAGESIZE, $start = NULL)
    {
        $searchUrl = self::getSearchUrl();
        $where['fl'] = implode(",", $fields);
        if (isset($start)) {
            $where['start_id'] = $start;
        } else {
            $where['page'] = $page;
        }
        $where['pagesize'] = $pagesize;
        $where = self::addCondition($where);
        $res = Curl::Request('POST', $searchUrl, ['form_params' => $where]);
        if ($res['json']['code'] == 1) {
            $res['json']['data']['is_over'] = count($res['json']['data']['rows']) < $pagesize ? 1 : 0;
            return $res['json']['data'];
        } else {
            return [
                'total' => 0,
                'is_over' => 1,
                'rows' => []
            ];
        }
    }

    private static function addCondition($where)
    {
        switch (static::SOLR_SECTION) {
            case 'goods' :
                if (HhzEnv::isProductionWithoutApi2() && gethostname() != 'hzv_biz_admin') {  //生产环境 除去 api2 与 商家后台
                    if (!isset($where['shop_id']) || isset($where['shop_id']['not'])) {
                        if (isset($where['shop_id']['not'])) {
                            $where['shop_id']['not'] = is_array($where['shop_id']['not']) ?
                                array_merge($where['shop_id']['not'], GoodsBasicInfo::$testShopIds)
                                :
                                array_merge([$where['shop_id']['not']], GoodsBasicInfo::$testShopIds);
                        } else {
                            $where['shop_id']['not'] = array_merge(GoodsBasicInfo::$testShopIds);
                        }
                        $where['shop_id']['not'] = array_unique($where['shop_id']['not']);
                    }
                    if (!isset($where['stock'])) {
                        $where['stock'] = ['gt' => 0];
                    }
                }
                break;
        }
        return $where;
    }
}